#include "comm.hpp"

int main()
{
    //1、创建公共的key值
    key_t k = ftok(PATH_NAME,PROJ_ID);
    if(k < 0)
    {
        Log("server:ftok failure!", Error)<<  "  server key: " << k << endl;
        exit(1);
    }
    Log("server:ftok success", Debug) <<  " server key: " << k << endl;

    //2、创建共享内存
    int shmid = shmget(k, SHM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if( shmid < 0)
    {
        Log("server: creat shm failure!", Error) << " the shmid:" << shmid << endl;
        exit(2);
    }
    Log("server: creat shm success", Debug) << " the shmid:" << shmid << endl;

    //3、建立映射（关联）
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);
    Log("server: attach shm success", Debug) << " the shmaddr: "<< shmaddr << endl;

    sleep(5);

    //4、进行通信


    //5、取消映射（取关联）
    int ret = shmdt(shmaddr);
    assert(ret != -1);
    (void)ret;
    Log("server: detach shm success", Debug) << " the ret: " << ret << endl;

    sleep(5);

    //6、删除共享内存
    ret = shmctl(shmid, IPC_RMID, nullptr);
    assert(ret != -1);
    (void)ret;
    Log("server: delete shm success", Debug) << "shmid: " << shmid << endl;


    return 0;
}